<template>
  <div class="census-manage">
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :option="option"
        :data="data"
        :page.sync="page"
        :search.sync="search"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="onLoad"
        @search-change="searchChange"
        v-model="item"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            @click="handleDown()"
            v-if="false"
            >查看异常详情</el-button
          >
          <el-button type="primary" size="small" @click="handleExportExcel()"
            >导出</el-button
          >
        </template>
        <template slot-scope="{ type, size, row }" slot="menu">
          <el-button
            v-if="row.interveneFlag == 'N'"
            icon="el-icon-view"
            :size="size"
            :type="type"
            @click="handleTodo(row)"
            >去处理</el-button
          >
          <el-button
            v-else
            icon="el-icon-view"
            :size="size"
            :type="type"
            @click="getToDetail(row)"
            >查看详情</el-button
          >
        </template>
      </avue-crud>
      <HandlePop
        ref="todoHandlePop"
        v-if="showHandlePop"
        :title="`项目名称：${popTitle}`"
        @close="showHandlePop = false"
        @submitSuccess="onLoad(page, searchForm)"
      />
    </basic-container>
  </div>
</template>

<script>
import { userpersonalsummaryList } from "@/api/report/report";
import HandlePop from "@/views/desk/todo/handlePop/index";
import { exportExcel } from "./config";
export default {
  name: "reportGrid",
  components: { HandlePop },
  data() {
    let _this = this;
    return {
      teamReport: false,
      item: {},
      user: {},
      search: {},
      answerData: [],
      sampleParams: {},
      projectId: "",
      answerDetailDialogView: false,
      selectionList: [],
      promote: true,
      searchParams: {},
      page: {
        pageSize: 10,
      },
      option: {
        // addBtnText: '添加类别',
        delBtnText: "删除",
        excelBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        border: true,
        selection: false,
        index: true,
        expandLevel: 3,
        headerAlign: "left",
        align: "left",
        tree: false,
        addBtn: false,
        labelWidth: 160,
        menuWidth: 100,
        searchMenuSpan: 4,
        height: 500,
        column: [
          {
            label: "学号",
            prop: "userCode",
            overHidden: true,
            width: 150,
            search: true,
            searchLabelWidth: 100,
          },
          {
            label: "姓名",
            prop: "realName",
            width: 150,
            overHidden: true,
            search: true,
            searchLabelWidth: 100,
          },
          {
            label: "性别",
            width: 150,
            overHidden: true,
            prop: "sex",
            searchLabelWidth: 100,
            search: true,
            type: "select",
            searchValue: _this.$route.query.sex || "",
            dicData: [
              {
                label: "男",
                value: "1",
              },
              {
                label: "女",
                value: "2",
              },
            ],
          },
          {
            label: "学校",
            overHidden: true,
            prop: "schoolName",
          },
          {
            label: "年级",
            width: 150,
            searchLabelWidth: 100,
            search: true,
            searchValue: _this.$route.query.gradeName || "",
            overHidden: true,
            searchMultiple: true,
            type: "select",
            prop: "gradeName",
            props: {
              label: "gradeName",
              value: "gradeName",
            },
            dicUrl: `/api/blade-system/grade/list`,
          },
          {
            label: "班级",
            overHidden: true,
            width: 150,
            prop: "className",
          },
          {
            label: "年度",
            prop: "yearly",
            search: true,
            searchLabelWidth: 100,
            overHidden: true,
            type: "select",
            props: {
              label: "yearly",
              value: "yearly",
            },
            dicUrl: "/api/blade-measure/measure/term/listAll",
          },
          {
            label: "学期",
            prop: "termId",
            search: true,
            searchLabelWidth: 100,
            overHidden: true,
            type: "select",
            props: {
              label: "term",
              value: "termId",
            },
            dicUrl: "/api/blade-measure/measure/term/listAll",
          },
          // {
          //   label: "测试项目",
          //   overHidden: true,
          //   width: 150,
          //   prop: "projectName",
          // },
          {
            label: "测试项目",
            // hide: true,
            width: 150,
            prop: "projectId",
            type: "select",
            searchLabelWidth: 100,
            search: true,
            dicUrl: `/api/blade-measure/measure/userpersonalsummary/project`,
            // dicQuery: {
            //   tenantId: _this.$route.query.tenantId,
            //   current: 1,
            //   size: 100,
            // },
            props: {
              label: "projectName",
              value: "projectId",
            },
          },
          {
            label: "测题发布时间",
            overHidden: true,
            width: 150,
            prop: "createAt",
          },
          {
            label: "干预结果",
            overHidden: true,
            searchLabelWidth: 100,
            searchValue: _this.$route.query.interveneFlag || "",
            width: 150,
            dicData: [
              {
                label: "已干预",
                value: "Y",
              },
              {
                label: "未干预",
                value: "N",
              },
            ],
            type: "select",
            search: true,
            prop: "interveneFlag",
          },
          {
            label: "测评总结果",
            overHidden: true,
            width: 150,
            search: true,
            type: "select",
            searchValue: _this.$route.query.level || "",
            prop: "level",
            searchMultiple: true,
            searchLabelWidth: 100,
            searchSpan: 12,
            dicUrl: "/api/blade-system/dict/dictionary?code=warning_level",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            // dicData: [
            //   {
            //     label: "一级预警",
            //     value: "1",
            //   },
            //   {
            //     label: "二级预警",
            //     value: "2",
            //   },
            //   {
            //     label: "三级预警",
            //     value: "3",
            //   },
            //   {
            //     label: "正常",
            //     value: "0",
            //   },
            // ],
          },

          {
            label: "是否专项测试",
            overHidden: true,
            searchLabelWidth: 100,
            searchValue: "1",
            width: 150,
            dicData: [
              {
                label: "是",
                value: "1",
              },
              {
                label: "否",
                value: "0",
              },
            ],
            type: "select",
            search: true,
            prop: "specialTypeId",
          },
        ],
      },
      data: [],
      loading: true,
      showHandlePop: false, // 是否显示待办弹窗
      popTitle: "",
      doneType: "undone",
      params: {},
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.projectId);
      });
      return ids.join(",");
    },
  },
  created() {},
  mounted() {
    let guidanceNote = ""; // 请求后台返回的内容字符串
    this.str = this.escapeStringHTML(guidanceNote);
    let params = Object.assign({}, this.$route.query);
    if (params.sex) {
      params.sex = params.sex == "1" ? 1 : 2;
    }
    if (params.level) {
      params.levels = params.level + "";
      delete params.level;
    }
    params.specialTypeId = "1";
    this.onLoad(this.page, params);
  },
  methods: {
    async onLoad(page = this.page, params = {}) {
      if (this.$route.query.projectId) {
        params.projectId = this.$route.query.projectId;
      }
      params.tenantId = this.$route.query.tenantId;
      if (params.level) {
        if (Array.isArray(params.level)) {
          params.levels = params.level.join(",");
        } else {
          params.levels = params.level;
        }
        delete params.level;
      }
      if (params.gradeName) {
        if (Array.isArray(params.gradeName)) {
          params.grades = params.gradeName.join(",");
        } else {
          params.grades = params.gradeName;
        }
        delete params.gradeName;
      }
      if (this.$route.query.projectId) {
        params.projectId = this.$route.query.projectId;
      }
      this.searchParams = params;
      this.params = params;
      const {
        data: { data },
      } = await userpersonalsummaryList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      );
      this.loading = false;
      this.data = data.records;
      this.data.map((item) => {
        item.level = item.level + "";
      });
      this.page.total = data.total;
      this.loading = false;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.searchParams);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page, this.searchParams);
    },
    async searchChange(params, done) {
      await this.onLoad(this.page, params);
      done();
    },
    handleDown() {
      this.$router.push({
        name: "异常详情",
        query: {
          projectId: this.$route.query.projectId,
          tenantId: this.$route.query.tenantId,
        },
      });
    },
    getToDetail(row) {
      // this.$router.push({
      //   name: "个人档案",
      //   query: {
      //     userId: row.userId,
      //   },
      // });
      this.showHandlePop = true;
      this.popTitle = row.projectName || "";
      this.$nextTick(() => {
        this.$refs["todoHandlePop"].show("warn", row, false);
      });
    },
    // 去处理
    handleTodo(rowData) {
      this.showHandlePop = true;
      this.popTitle = rowData.projectName || "";
      this.$nextTick(() => {
        this.$refs["todoHandlePop"].show("warn", rowData, true);
        // this.$refs["todoHandlePop"].show(
        //     this.activeNames,
        //     rowData,
        //     this.doneType === "undone"
        //   );
      });
    },
    handleExportExcel() {
      exportExcel(Object.assign(this.params, this.query));
    },
    escapeStringHTML(str) {
      //把实体格式字符串转义成HTML格式的字符串
      str = str.replace(/&lt;/g, "<");
      str = str.replace(/&gt;/g, ">");
      return str;
    },
  },
};
</script>

<style lang="scss" scoped>
.census-manage {
  .container {
    height: 70vh;
    overflow-y: auto;
  }
  .dialogInfoCss {
    overflow-y: hidden;
    margin-top: -50px;
    // margin-bottom: 40px;
  }
}
</style>
